package cn.quevo.cube.provide.define.validate.impl;

import cn.quevo.common.lang.NumberUtils;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.cube.provide.define.entity.CubeColumnSequence;
import cn.quevo.cube.provide.define.entity.dto.CubeColumnDto;
import cn.quevo.cube.provide.define.enums.SequenceType;
import cn.quevo.cube.provide.define.validate.ColumnTypeValidate;
import cn.quevo.cube.provide.exception.SequenceException;

/**
 * 自增序列验证
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/14 4:57 PM
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
public class AutoSequenceValidate extends SequenceValidate implements ColumnTypeValidate {

    @Override
    protected void checked(CubeColumnDto cubeColumnDto) {
        String start = super.getCustomParam("startValue");
        String step = super.getCustomParam("stepInt");
        boolean isNull = StringUtils.isBlank(start) || StringUtils.isBlank(step);
        if (isNull) {
            throw new SequenceException();
        }
        long startValue = NumberUtils.toLong(start);
        long stepValue = NumberUtils.toLong(step);
        if (startValue <= 0 || stepValue <= 0) {
            throw new SequenceException();
        }
        this.initSequence(cubeColumnDto, startValue, stepValue);
    }

    /**
     * 初始化序列信息
     *
     * @param cubeColumnDto
     * @author July july_sky@foxmail.com
     * @date 2018-04-20 17:24:10
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    private void initSequence(CubeColumnDto cubeColumnDto, long startValue, long stepValue) throws SequenceException {
        super.initSequence(cubeColumnDto);
        cubeColumnDto.setCubeColumnSequence(new CubeColumnSequence());
        cubeColumnDto.getCubeColumnSequence().setStartValue(startValue);
        cubeColumnDto.getCubeColumnSequence().setCurrentValue(startValue);
        cubeColumnDto.getCubeColumnSequence().setStepInt(stepValue);
        cubeColumnDto.getCubeColumnSequence().setSequenceTypeCd(SequenceType.AUTO.getCode());
    }
}
